package disjunction.examples.states
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.geom.Vector3D;
	import flash.utils.Dictionary;
	
	import recursivecontrol.TimedController;
	import recursivecontrol.extensions.view.BitmapDataSpriteSheet;
	import recursivecontrol.extensions.view.BitmapSprite;
	import disjunction.extensions.view.BlitterViewConfig;
	import disjunction.extensions.view.GraphicsViewConfig;
	
	public class PlayView extends Controller
	{
		private const BACKGROUND_COLOUR:uint = 0x0000ff;
		
		private var _context:DisplayObjectContainer;
		
		private var _blitmap:Bitmap;
		private var _buffer:BitmapData;
		private var _scale:Number;
		
		private var _offset:Vector3D = new Vector3D();
		
		private var _testBmd:BitmapData; //DEV
		[Embed("assets/monolith.png")] private var MonolithGraphic:Class;
		
		private var _sprites:Dictionary = new Dictionary();
		
		/** map sprite type names to spritesheets */
		private var _spriteSheets:Object = {};
		/**
		 * Create a new camera.
		 * 
		 * @param fps Frames per second to render at.
		 * @param width The width of the Camera.
		 * @param height The height of the Camera.
		 * @param scale The number of display pixels to world units,
		 * for all displayed objects. Allows pixel-doubling.
		 */
		
		public override function initialise(config:Object):void
		{
			super.initialise(config);
			_scale = (config as BlitterViewConfig).pixelScale;
			
			_context = ((config as BlitterViewConfig).viewContext) as DisplayObjectContainer;
			_buffer = new BitmapData(config.width, config.height, false, 0x00FF00);
			_blitmap = new Bitmap(_buffer);
			_blitmap.scaleX = _blitmap.scaleY = _scale;
			_context.addChild(_blitmap);
			//event listener for when this has been added? (this comment from erstwhile constructor)
			//_blitmap.addEventListener(Event.ADDED, centreBoundaries);
			
			_testBmd = (new MonolithGraphic()).bitmapData;
			
			//create a test sprite
			

		}
		
		
		/**
		 * Update the display according to time passed since last update.
		 * @param
		 */
		public override function update(timeDeltaMs:uint = 0):void
		{
			//_buffer.lock();
			//trace('update view');
			//trace(parent);
			//do ops
			//_buffer.setPixel((_model as PlayModel).test, 10, 0xFF00FF);
			_buffer.fillRect(new Rectangle(0,0,320,240), 0x0000FF);
			_buffer.copyPixels(_testBmd, new Rectangle(0,0,32,32), new Point(0, 0));
			
			var entity:Object = (model as PlayModel).entity;
			//for each (entity in model.entities)
			//{
				var sprite:BitmapSprite;
				//create a sprite for this if not existing
				if (!_sprites[entity])
				{
					//1. Need sprite to have a type(String) so that we can use this to get the right
					// a)spriteSheet
					// b)pose string
					//...For BitmapDataSprite's ctor.
					
					var spriteSheet:BitmapDataSpriteSheet;
					spriteSheet = new BitmapDataSpriteSheet(32, 32, 1, 1);
					spriteSheet.addAnimationFrame("restpose", _testBmd);
					_spriteSheets["test"] = spriteSheet;
					_sprites[entity] = new BitmapSprite(spriteSheet, "restpose");
					trace("added sprite");
				}
				sprite = _sprites[entity];
				
				trace("sprite");
				trace(sprite);
				trace(_buffer);
				trace(_sprites[entity]);
				trace(entity);
				//trace(entity.transform.translation.x);
				//trace(entity.transform.translation.y);
				
				sprite.drawTo(_buffer, new Point((model as PlayModel).test, (model as PlayModel).test));
				
				//_buffer.copyPixels(entity.sprite.currentFrame, new Rectangle(0,0,32,32), new Point((_model as PlayModel).test, 10));
			//}
			
			//_buffer.unlock(); //changeRect param?
		}
	}
}